﻿package app.view.components.header
{
	import app.model.types.LangTypes;
	import app.view.components.buttons.SelectableButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class LangsBarBase extends Sprite
	{
		public var rusBtn:SelectableButton;
		public var engBtn:SelectableButton;
		public var chinaBtn:SelectableButton;
		
		private var _current:SelectableButton;
		private var _lang:String;
		private var _btns:/*SelectableButton*/Array;
		
		public function LangsBarBase() 
		{
			super();
			init();
		}
		
		private function init():void
		{
			rusBtn.data = LangTypes.RUSSIAN;
			engBtn.data = LangTypes.ENGLISH;
			chinaBtn.data = LangTypes.CHINA;
			_btns = [rusBtn, engBtn, chinaBtn];
			
			for each(var i:SelectableButton  in _btns) 
			{
				i.addEventListener(MouseEvent.CLICK, onBtnClick);
			};
			lang = LangTypes.DEFAULT;
		};
		
		private function onBtnClick(event:MouseEvent):void 
		{
			var t:SelectableButton = SelectableButton(event.target);
			lang = String(t.data);
			dispatchEvent( new Event( Event.CHANGE));
			//dispatchEvent( new UIEvent( UIEvent.LANG_CHANGE, true, true, lang));
		};
		
		public function get lang():String { return _lang; };
		
		public function set lang(value:String):void 
		{
			_lang = value;
			////trace('_lang : ' + _lang);
			if ( _current ) _current.selected = false;
			for each(var i:SelectableButton  in _btns) 
			{
				if ( String(i.data) == value) 
				{
					_current = i;
					_current.selected =  true;
					return;
				};
			};
			
			//dispatchEvent( new Event( Event.CHANGE ));
		};
	};
};